﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;

namespace SmartMD.Manager.Report
{
    public class PrintService
    {
        // Declare the PrintDocument object.
        private List<SpecPrint> _specPrintList = null;
        private System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();//创建一个PrintDocument的实例
        int index = 0;

        public PrintService()
        {
            //设置打印样式
            docToPrint.DefaultPageSettings.Landscape = false;  //(True为横向，False为竖向)
            docToPrint.DefaultPageSettings.PaperSize = new PaperSize("Default", 300,100); //其中的数字为英寸

            this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
        }//将事件处理函数添加到PrintDocument的PrintPage中


        // This method will set properties on the PrintDialog object and
        // then display the dialog.
        public void StartPrintPreview(List<SpecPrint> specToPrints)
        {
            if (specToPrints == null)
                return;
            _specPrintList = specToPrints;

            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();//创建一个PrintDialog的实例。
            PrintDialog1.AllowSomePages = true;
            // Show the help button.
            PrintDialog1.ShowHelp = true;

            PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
            DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框
            if (result == DialogResult.OK)
            {
                PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
                printPreviewDialog.Document = docToPrint;
                //printPreviewDialog.Height = 50;
                //printPreviewDialog.Width = 200;
                printPreviewDialog.ShowDialog();
                //docToPrint.Print();//开始打印
            }
        }

        // The PrintDialog will print the document
        // by handling the document's PrintPage event.
        private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)//设置打印机开始打印的事件处理函数
        {
            System.Drawing.Font printFont = new System.Drawing.Font("宋体", 5, System.Drawing.FontStyle.Regular);
            int leftMargin = 5;
            int topMargin = 5;
            SpecPrint specprint;
            if (index >= _specPrintList.Count)
            {
                e.HasMorePages = false;
                return;
            }
            specprint = _specPrintList[index];
            e.Graphics.DrawString("所属患者：", printFont, System.Drawing.Brushes.Black, leftMargin, topMargin);
            e.Graphics.DrawString(specprint.PatientName, printFont, System.Drawing.Brushes.Black, leftMargin + 40, topMargin);
            e.Graphics.DrawString("标本类型：", printFont, System.Drawing.Brushes.Black, leftMargin, topMargin + 10);
            e.Graphics.DrawString(specprint.SpecType, printFont, System.Drawing.Brushes.Black, leftMargin + 40, topMargin + 10);
            e.Graphics.DrawString("采集时间：", printFont, System.Drawing.Brushes.Black, leftMargin, topMargin + 20);
            e.Graphics.DrawString(specprint.StoreDate.ToString(), printFont, System.Drawing.Brushes.Black, leftMargin + 40, topMargin + 20);            
            System.Drawing.Font ft2 = new System.Drawing.Font("Code 128", 64, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
            System.Drawing.Brush br = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
            e.Graphics.DrawString(Get128CodeString(specprint.SpecId), ft2, br, -20, topMargin + 23);
            e.Graphics.DrawString(specprint.SpecId, printFont, br, leftMargin, topMargin + 85);

            //当PrintPage函数执行完后，如果HasMorePages==true，则重新执行一遍PrintPage这个函数
            if (index++ >= _specPrintList.Count - 1)
            {
                e.HasMorePages = false;
                index = 0;
            }
            else
            {
                e.HasMorePages = true;
            }
        }


        //英寸换算到厘米
        public decimal FromInchToCM(decimal inch)
        {
            return Math.Round((System.Convert.ToDecimal((inch / 100)) * System.Convert.ToDecimal(2.5400)), 2);  //Math.Round取两位小数
        }

        //一维码
        private string Get128CodeString(string inputData)
        {
            string result;
            int checksum = 104;
            for (int ii = 0; ii < inputData.Length; ii++)
            {
                if (inputData[ii] >= 32)
                {
                    checksum += (inputData[ii] - 32) * (ii + 1);
                }
                else
                {
                    checksum += (inputData[ii] + 64) * (ii + 1);
                }
            }
            checksum = checksum % 103;
            if (checksum < 95)
            {
                checksum += 32;
            }
            else
            {
                checksum += 100;
            }
            result = Convert.ToChar(204) + inputData.ToString() + Convert.ToChar(checksum) + Convert.ToChar(206);
            return result;
        }
    }
}
